<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('Vision');
Globals::requireTable('User');
Globals::requireTable('Order');
Globals::requireTable('Account');
Globals::requireTable('Vlevel');

class IndexController extends Controller
{
	protected $VisionTable;
	protected $UserTable;
	protected $OrderTable;
	protected $AccountTable;
	protected $VlevelTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->VisionTable = new VisionTable($config);
		$this->UserTable = new UserTable($config);
		$this->OrderTable = new OrderTable($config);
		$this->AccountTable = new AccountTable($config);
		$this->VlevelTable = new VlevelTable($config);
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	public function visionAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','vision'=>'','v-money'=>'','v-type'=>'2'));
	        exit();
	    }
	    
	    $vcount = 0;
	    $data = $this->VlevelTable->listAllWithFields('level',array('uid'=>$uid));
	    foreach ($data as $k=>$v){
	        $vcount += $v['level'];
	    }
	    
	    if($vcount <= 0){
	        echo json_encode(array('returncode'=>'-2','vision'=>'','v-money'=>'','v-type'=>'2'));
	        exit();
	    }
	    
	    $vision = $this->VisionTable->getRow(array('status'=>1),'id DESC');
	    echo json_encode(array('returncode'=>'0','vision'=>$vision['vision'],'v-money'=>$vcount,'v-type'=>'2'));
	    exit();
	}
	
	public function checkendtimeAction(){
	    $update_id = array();
	    
	    $account = $this->AccountTable->listAll(array('status'=>2));
	    foreach ($account as $k=>$v){
	        if($v['endtime'] < time()){
	            $user = $this->UserTable->getRow($v['uid']);
	            $orders = $this->OrderTable->listAll(array('aid'=>$v['id'],'status'=>1));
	            foreach ($orders as $kk=>$vv){
	                $this->OrderTable->update(array('status'=>2),$vv['id']);
	                
	                
	            }
	            
	            
	            $this->AccountTable->update(array('status'=>3),$v['id']);
	        }
	    }
	    
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('IndexController', 'Controller');
